import { useState } from 'react'
import { getMenuApi } from '@/api/auth/menu'
import { MenuSearchParams } from '@/interface/auth/menu'
import { PaginatedParams } from 'ahooks/lib/useAntdTable'
import { antdTableResult } from '@/interface/ahooks'
import { getTreeSelectData } from '@/utils/antd'

export function useMenuTable() {
  const [treeOptions, setTreeOptions] = useState([])
  const getTreeSelect = (tableData) => {
    // 过滤treeSelect数据的函数
    const selectData = getTreeSelectData(tableData, 'name', 'id')
    setTreeOptions(selectData)
  }
  const getMenuData = async (
    { current, pageSize }: PaginatedParams[0],
    formData: MenuSearchParams,
  ): Promise<antdTableResult<any>> => {
    const params: any = { ...formData }
    params.current = current
    params.size = pageSize
    const res: any = await getMenuApi(params)
    getTreeSelect(res)
    return {
      total: res.length,
      list: res,
    }
  }
  return { getMenuData, treeOptions }
}
